package ar.com.ford.it.fullEconomics.logic;

import java.util.Collection;
import ar.com.ford.it.fullEconomics.FullEconomicsPrs;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.persistence.dao.ColorDao;
import ar.com.ford.it.fullEconomics.persistence.dto.Ncas003ColorVw;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;

public class ColorLogic {

    private static final String CLASS_NAME = ColorLogic.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);
    /**
     * Busca todos los colores de los autos
     * @return
     * @throws FullEconomicsException 
     */
    public Collection<Ncas003ColorVw> findAllColor() throws FullEconomicsException {
        String METHOD_NAME = "findAllColor";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Ncas003ColorVw> res = null;
        
        try {
            res = (new ColorDao()).findAllColor();
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Find all Color Exception", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Color.FINDALL);
        }

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }
    /**
     * Obtiene el color segun el id
     * @param code
     * @return
     * @throws FullEconomicsException
     */
    public Ncas003ColorVw getColorById(String code) throws FullEconomicsException{
        String METHOD_NAME = "getColorById";
        log.entering(CLASS_NAME, METHOD_NAME);
        Ncas003ColorVw res=null;
        
        try {
            res = (new ColorDao()).getColorById(code);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Find all Color Exception", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Color.GETBYID);
        }
        
        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;

    }
    /**
     * Compara el objeto leido.
     * @param colorRead
     * @throws FullEconomicsException
     */
    public void mergeToObjCas(Ncas003ColorVw colorRead) throws FullEconomicsException {
        String METHOD_NAME = "comparteToObjCas";
        log.entering(CLASS_NAME, METHOD_NAME);
        Ncas003ColorVw objDataBase=null;
        
        try {
            PersistenceUtil.newTransaction();
            PersistenceUtil.getPersistenceManager().clearCache();
            objDataBase = (new ColorDao()).getColorForUpdate(colorRead.getCas003ColorCodeC());
            if(!objDataBase.getCas003ColorInDateY().equals(colorRead.getCas003ColorInDateY())){
                objDataBase.setCas003ColorInDateY(colorRead.getCas003ColorInDateY());
            }
            if(!objDataBase.getCas003ColorNameN().equals(colorRead.getCas003ColorNameN())){
                objDataBase.setCas003ColorNameN(colorRead.getCas003ColorNameN());
            }
            if(!objDataBase.getCas003ColorOutDateY().equals(colorRead.getCas003ColorOutDateY())){
                objDataBase.setCas003ColorOutDateY(colorRead.getCas003ColorOutDateY());
            }
            if(!objDataBase.getCas003ColorStatusFlagF().equals(colorRead.getCas003ColorStatusFlagF())){
                objDataBase.setCas003ColorStatusFlagF(colorRead.getCas003ColorStatusFlagF());
            }
            if(!objDataBase.getCas003LastUpdtS().equals(colorRead.getCas003LastUpdtS())){
                objDataBase.setCas003LastUpdtS(colorRead.getCas003LastUpdtS());
            }
            if(!objDataBase.getCas003LastUpdtUserC().equals(colorRead.getCas003LastUpdtUserC())){
                objDataBase.setCas003LastUpdtUserC(colorRead.getCas003LastUpdtUserC());
            }

            PersistenceUtil.commitTransaction();
            

        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Merge Color Exception", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Color.MERGE);
        }
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Crea el color
     * @param colorRead
     * @throws FullEconomicsException
     */
    public void createColor(Ncas003ColorVw colorRead) throws FullEconomicsException {
        String METHOD_NAME = "createColor";
        log.entering(CLASS_NAME, METHOD_NAME);
        try {
            (new ColorDao()).createColor(colorRead);
        } catch (PersistenceException e) {
            log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME,
                    "Create Color Exception", e);
            throw new FullEconomicsException(e,
                    FullEconomicsPrs.MessageErrors.Color.CREATE);
        }
        
        log.exiting(CLASS_NAME, METHOD_NAME);
    }

}
